var a = getApp(), e = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js");

Page({
    data: {
        isLoading: !1,
        page: 1,
        addresses: [],
        fromPage: "my",
        total: 0,
        winWidth: a.globalData.windowWidth
    },
    onLoad: function(a) {
        var e = a.from;
        e && this.setData({
            fromPage: e
        }), this.loadAddress(1);
    },
    onShow: function() {
        this.loadAddress(1);
    },
    onPullDownRefresh: function() {
        this.loadAddress(1);
    },
    onReachBottom: function() {
        this.loadAddress(this.data.page + 1);
    },
    addAddress: function() {
        wx.navigateTo({
            url: "/pages/address/add"
        });
    },
    editAddress: function(e) {
        var d = e.currentTarget.dataset.addressId, t = !0, s = !1, r = void 0;
        try {
            for (var i, o = this.data.addresses[Symbol.iterator](); !(t = (i = o.next()).done); t = !0) {
                var n = i.value;
                if (1 * n.address_id == 1 * d) {
                    a.globalData.editAddressItem = n;
                    break;
                }
            }
        } catch (a) {
            s = !0, r = a;
        } finally {
            try {
                !t && o.return && o.return();
            } finally {
                if (s) throw r;
            }
        }
        wx.navigateTo({
            url: "/pages/address/add?addressId=" + d
        });
    },
    tapDelAddress: function(a) {
        var e = this, d = a.currentTarget.dataset.addressId;
        wx.showModal({
            title: "是否删除这个地址？",
            content: "删除后不能恢复",
            confirmText: "不删除",
            cancelText: "删除",
            success: function(a) {
                a.cancel && e.delAddress(d);
            }
        });
    },
    delAddress: function(a) {
        var d = this;
        wx.showLoading({
            mask: !0
        }), e.delete("/v2/address/delete", {
            address_id: a
        }).then(function(e) {
            if (wx.hideLoading(), 1001 === e.data.code) {
                for (var t = d.data.addresses, s = 0, r = t.length; s < r; s++) if (1 * t[s].address_id === a) {
                    t.splice(s, 1);
                    break;
                }
                d.setData({
                    addresses: t,
                    total: d.data.total - 1
                });
            }
        }).catch(function() {
            wx.hideLoading();
        });
    },
    setDefaultAddress: function(a) {
        var d = this, t = 1 * a.currentTarget.dataset.addressId, s = 1 * a.currentTarget.dataset.isDefault;
        wx.showLoading({
            mask: !0
        }), e.post("/v2/address/set_default", {
            address_id: t,
            address_is_default: 0 === s ? 1 : 0
        }).then(function(a) {
            wx.hideLoading(), 1001 === a.data.code && d.sortAddress(d.data.addresses, t, s);
        }).catch(function() {
            wx.hideLoading();
        });
    },
    sortAddress: function(a, e, d) {
        for (var t = void 0, s = 0, r = a.length; s < r; s++) if (0 === d) a[s].address_id === e ? (a[s].address_is_default = 1, 
        t = s) : a[s].address_is_default = 0; else if (a[s].address_id === e) {
            a[s].address_is_default = 0;
            break;
        }
        var i = [];
        if (0 === d) {
            var o = a[t];
            a.splice(t, 1), (i = a.sort(function(a, e) {
                return e.address_id - a.address_id;
            })).unshift(o);
        } else i = a.sort(function(a, e) {
            return e.address_id - a.address_id;
        });
        this.setData({
            addresses: i
        });
    },
    chooseAddress: function(e) {
        var d = this, t = this.data.fromPage;
        if ("order" === t || "order_detail" === t) if ("order_detail" === t) wx.showModal({
            title: "提示",
            content: "确认后将无法修改该地址，如需修改请联系客服。",
            success: function(t) {
                if (t.confirm) {
                    var s = e.currentTarget.dataset.addressId, r = !0, i = !1, o = void 0;
                    try {
                        for (var n, l = d.data.addresses[Symbol.iterator](); !(r = (n = l.next()).done); r = !0) {
                            var c = n.value;
                            if (1 * c.address_id == 1 * s) {
                                a.globalData.chooseAddressItem = c;
                                break;
                            }
                        }
                    } catch (a) {
                        i = !0, o = a;
                    } finally {
                        try {
                            !r && l.return && l.return();
                        } finally {
                            if (i) throw o;
                        }
                    }
                    wx.navigateBack({
                        delta: 1
                    });
                }
            }
        }); else {
            var s = e.currentTarget.dataset.addressId, r = !0, i = !1, o = void 0;
            try {
                for (var n, l = this.data.addresses[Symbol.iterator](); !(r = (n = l.next()).done); r = !0) {
                    var c = n.value;
                    if (1 * c.address_id == 1 * s) {
                        a.globalData.chooseAddressItem = c;
                        break;
                    }
                }
            } catch (a) {
                i = !0, o = a;
            } finally {
                try {
                    !r && l.return && l.return();
                } finally {
                    if (i) throw o;
                }
            }
            wx.navigateBack({
                delta: 1
            });
        }
    },
    loadAddress: function(d) {
        var t = this;
        this.data.isLoading ? wx.stopPullDownRefresh() : (this.setData({
            isLoading: !0
        }), e.get("/v2/address/mine", {
            page: d
        }).then(function(e) {
            if (wx.stopPullDownRefresh(), t.setData({
                isLoading: !1
            }), 1001 === e.data.code) {
                var s = t.data.addresses, r = e.data.response.data || [];
                s = 1 === d ? r : s.concat(r), t.setData({
                    addresses: s,
                    page: r.length > 0 ? d : d - 1,
                    total: e.data.response.total
                }, function() {
                    s.length < 1 && (a.globalData.chooseAddressItem = null);
                });
            }
        }).catch(function() {
            wx.stopPullDownRefresh(), t.setData({
                isLoading: !1
            });
        }));
    }
});